package com.study.ws.config.topmp;

import org.springframework.boot.task.ThreadPoolTaskSchedulerBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @author xl-9527
 * @since 2025/3/23
 **/
@EnableWebSocketMessageBroker
@Configuration(proxyBeanMethods = false)
public class StompConfiguration implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(final StompEndpointRegistry registry) {
        registry.addEndpoint("/stomp");
    }

    @Override
    public void configureMessageBroker(final MessageBrokerRegistry registry) {
        final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskSchedulerBuilder()
                .poolSize(2)
                .threadNamePrefix("stomp-task-heartbeat")
                .build();
        taskScheduler.initialize();

        registry.setApplicationDestinationPrefixes("/app")
                .enableSimpleBroker("/topic", "/user")
                .setHeartbeatValue(new long[]{10000, 10000})
                .setTaskScheduler(taskScheduler);
    }
}
